package com.atguigu.dga.assessor.quality;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.assessor.Assessor;
import com.atguigu.dga.dolphinscheduler.bean.TDsTaskInstance;
import com.atguigu.dga.dolphinscheduler.service.TDsTaskInstanceService;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.Days;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component("TIME_LINESS")
public class TimeLineAssessor extends Assessor {
    @Autowired
    TDsTaskInstanceService tDsTaskInstanceService;
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        //排除ods层的表
        if (assessParam.getTableMetaInfo().getTableMetaInfoExtra().getDwLevel().equals("ODS")){
            return;
        }
        //任务名
        String tsetName=assessParam.getTableMetaInfo().getSchemaName()+"."+assessParam.getTableMetaInfo().getTableName();
        String assessDate = assessParam.getAssessDate();

        //参数信息
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSON.parseObject(metricParamsJson);
        String paramdays = jsonObject.getString("days");
        Integer parampercent = jsonObject.getInteger("percent");

        //计算当日产出时效
        long startTime = assessParam.getTDsTaskInstance().getStartTime().getTime();
        long endTime = assessParam.getTDsTaskInstance().getEndTime().getTime();
        long currentTime=(endTime-startTime)/1000;

        //计算前days天的平均产出时效
        //计算开始时间
        Date parseDate = DateUtils.parseDate(assessDate, "yyyy-MM-dd");
        Date addDays = DateUtils.addDays(parseDate, -Integer.valueOf(paramdays));
        String startformat = DateFormatUtils.format(addDays, "yyyy-MM-dd");

        //获取前days天的对象
        List<TDsTaskInstance> tDsTaskInstances = tDsTaskInstanceService.selectBeforeNDaysInstance(tsetName, startformat, currentTime);

        //统计时间和
        long totalDUraton=0;
        if (tDsTaskInstances.size()>0){
            //计算平均时间
            for (TDsTaskInstance tDsTaskInstance : tDsTaskInstances) {
                totalDUraton+=(tDsTaskInstance.getEndTime().getTime()-tDsTaskInstance.getStartTime().getTime());
            }
            long avgTime=totalDUraton/tDsTaskInstances.size()/1000;

            //判断是否超过范围
            if(currentTime > (avgTime+avgTime*parampercent/100)){
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                governanceAssessDetail.setAssessProblem("当日产出时效超过前" + paramdays + "天平均产出时效的" + parampercent + "%");
                governanceAssessDetail.setAssessComment("当日产出时效: " +  currentTime + " , 前"+ tDsTaskInstances.size() +"天的平均产出时效: " + avgTime);

            }
        }



    }
}
